//
//  YuanXinPlayerManager.m
//  yuanxinplayer
//
//  Created by 晏德智 on 2016/11/23.
//  Copyright © 2016年 晏德智. All rights reserved.
//

#import "YuanXinPlayerManager.h"
#import "YuanXinPlayer.h"

#if __has_include(<React/RCTBridge.h>)
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUIManager.h>
#import <React/RCTUtils.h>
#else
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import "RCTUIManager.h"
#import "RCTUtils.h"
#endif

@interface YuanXinPlayerManager()

@end

@implementation YuanXinPlayerManager

RCT_EXPORT_MODULE();

- (UIView *)view{
    // return  NULL;
    YuanXinPlayer *view =  [[YuanXinPlayer alloc] init];
    return view;
}

RCT_EXPORT_VIEW_PROPERTY(url, NSURL)
RCT_EXPORT_VIEW_PROPERTY(localPath, NSString)
RCT_EXPORT_VIEW_PROPERTY(contentInset, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(shouldAutoplay, BOOL)
RCT_EXPORT_VIEW_PROPERTY(framedrop, int)

RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPlayBackState, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onTriggerGetCurrentPlaybackTime, RCTDirectEventBlock)

RCT_EXPORT_METHOD(prepare:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
            [view prepare];
        }
    }];
}

RCT_EXPORT_METHOD(play:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
            [view play];
        }
    }];
}


RCT_EXPORT_METHOD(stop:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
            [view stop];
        }
    }];
}

RCT_EXPORT_METHOD(resume:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
            [view resume];
        }
    }];
}

RCT_EXPORT_METHOD(pause:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
            [view pause];
        }
    }];
}

RCT_EXPORT_METHOD(shutdown:(nonnull NSNumber *)reactTag)
{
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
            [view shutdown];
        }
    }];
}

RCT_EXPORT_METHOD(seekTo:(nonnull NSNumber *)reactTag playBackTime:(double)currentPlaybackTime) {
    
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
             [view seekTo:currentPlaybackTime];
        }
    }];
}

RCT_EXPORT_METHOD(getCurrentPlaybackTime:(nonnull NSNumber *)reactTag) {
    
    [self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, RCTView *> *viewRegistry) {
        YuanXinPlayer *view = (YuanXinPlayer *)viewRegistry[reactTag];
        if (![view isKindOfClass:[YuanXinPlayer class]]) {
            RCTLogError(@"Invalid view returned from registry, expecting YuanXinPlayer, got: %@", view);
        } else {
            NSTimeInterval currentPlaybackTime = [view getCurrentPlaybackTime];
            if (view.onTriggerGetCurrentPlaybackTime) {
                view.onTriggerGetCurrentPlaybackTime(@{@"result": @(currentPlaybackTime)});
            }
        }
    }];
}

@end
